package com.matt.service.impl;

import com.google.common.collect.Lists;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.domain.ChildrenExtendInfo;
import com.matt.domain.Classes;
import com.matt.domain.course.*;
import com.matt.domain.rel.RelTeacherCoursePlanAttended;
import com.matt.foundation.DO.*;
import com.matt.foundation.DO.course.CoursePlanClassesDO;
import com.matt.foundation.DO.course.PadCourseTemplate3Vo;
import com.matt.foundation.constant.*;
import com.matt.foundation.query.AreaCourseQuery;
import com.matt.foundation.query.ClassesCoursePlanQuery;
import com.matt.foundation.query.CoursePlanQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.ClassesChildrenListVo;
import com.matt.foundation.vo.course.*;
import com.matt.repository.ChildrenExtendInfoRepository;
import com.matt.repository.ClassesRepository;
import com.matt.repository.CourseTemplate3Repository;
import com.matt.repository.NoticeMessageRepository;
import com.matt.repository.base.*;
import com.matt.repository.course.*;
import com.matt.service.*;
import com.matt.service.remoteCall.SsoCallService;
import com.tuo51.foudation.utils.StaffDTOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

import static com.matt.foundation.model.ErrorCode.*;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/5
 * *******************************
 */
@Service
@Slf4j
public class CourseEarlyServiceImpl implements CourseEarlyService {

    @Autowired
    private CoursePlanTeachingAreaRepository coursePlanTeachingAreaRepository;

    @Autowired
    private ChildChildrenCourseItemRepository childChildrenCourseItemRepository;

    @Autowired
    private ChildrenCourseItemRepository childrenCourseItemRepository;

    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private ClassesRepository classesRepository;

    @Autowired
    private ChildChildrenExtendRepository childChildrenExtendRepository;

    @Autowired
    private ChildrenExtendInfoRepository childrenExtendInfoRepository;

    @Autowired
    private ChildrenCourseRepository childrenCourseRepository;

    @Autowired
    private ClassesCoursePlanRepository classesCoursePlanRepository;

    @Autowired
    private PrepareCourseRepository prepareCourseRepository;

    @Autowired
    private CoursePlanClassesRepository coursePlanClassesRepository;

//    @Autowired
//    private CoursePlanTopicRepository coursePlanTopicRepository;
//
//    @Autowired
//    private CourseTemplate1Repository courseTemplate1Repository;
//
//    @Autowired
//    private CourseTemplate2Repository courseTemplate2Repository;

    @Autowired
    private CourseTemplate3Repository courseTemplate3Repository;
   
    @Autowired
    private ClassesChildrenRepository classesChildrenRepository;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private ChildrenCourseService childrenCourseService;

    @Autowired
    private NoticeMessageRepository noticeMessageRepository;


    @Override
    public List<CoursePlanEarlyListVo> getPlanByAreaBetweenDays(String beginDate, String endDate, String areaId) {
        CoursePlanQuery query = CoursePlanQuery.newCoursePlanQuery().begin(beginDate).end(endDate).areaAt(areaId).pageSize(1000).build();
        List<CoursePlanDO> coursePlans = coursePlanTeachingAreaRepository.findCoursePlanByCondition(query);

        List<String> coursePlanIds = coursePlans.stream().map(k->k.getId()).collect(Collectors.toList());
        Map<String, Integer> cache = childChildrenCourseItemRepository.countChildrenByCoursePlanIds(coursePlanIds);

        List<CoursePlanEarlyListVo> myCourseListVoList = Lists.newArrayList();
        coursePlans.forEach(o -> {
            Integer count = cache.get(o.getId());
            int c = count==null ? 0 : count;
            myCourseListVoList.add(CoursePlanEarlyListVo.planOf(o, c + 0l));
        });

        return myCourseListVoList;
    }


    @Transactional
    @Override
    public Boolean delete(String coursePlanClassesId) {
        CoursePlanClasses coursePlanClasses = coursePlanClassesRepository.getOne(coursePlanClassesId);
        if (coursePlanClasses.getCourseStatus() != CourseEarlyConstant.CourseStatus.STARTING.codeOf())
            throw new BusinessException(COURSE_EARLY_CAN_NOT_DELETE, "课程已上，无法删除");
        coursePlanClassesRepository.deleteById(coursePlanClassesId);
        prepareCourseRepository.deleteByCoursePlanClassesId(coursePlanClassesId);
        childrenCourseItemRepository.deleteByCoursePlanId(coursePlanClassesId);
        noticeMessageRepository.deleteByLinkIdAndCategoryIn(coursePlanClassesId, Lists.newArrayList(NoticeMessageConstant.Category.EARLY_COURSE_PREPARE.codeOf(), NoticeMessageConstant.Category.EARLY_COURSE_AUDIT.codeOf(), NoticeMessageConstant.Category.EARLY_COURSE_REJECTED.codeOf()));
        return true;
    }

    @Override
    public Map childrenList(String coursePlanClassesId) {
        CoursePlanClasses coursePlanClasses = coursePlanClassesRepository.getOne(coursePlanClassesId);

        Classes classes = classesRepository.getOne(coursePlanClasses.getClassesId());

        List<ChildrenCourseItemDO> planedChildren = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(coursePlanClassesId);
        List<ClassesChildrenListVo> classesChildren = childrenService.findClassHourChildrenByClasses(coursePlanClasses.getClassesId());
        List<ChildrenListDO> monthChildren = childChildrenExtendRepository.findChildrenByEarlyStatusActive(coursePlanClasses.getAreaId()).stream().filter(o -> DateUtil.isInTime(o.getBirthday(), classes.getBeginMonth(), classes.getEndMonth())).collect(Collectors.toList());

    /*    for (ClassesChildrenListVo child : classesChildren) {
            ChildrenListDO vo = matchAllChild(child, monthChildren);
            if (Objects.nonNull(vo))
                monthChildren.remove(vo);
        }*/
        Iterator<ClassesChildrenListVo> childList = classesChildren.iterator();
        while(childList.hasNext()){
            ClassesChildrenListVo child = childList.next();
            ChildrenListDO vo = matchAllChild(child, monthChildren);
            if (Objects.nonNull(vo))
                monthChildren.remove(vo);

        }

        Map response = new HashMap();
        response.put("planedChildren", planedChildren);
        response.put("classesChildren", classesChildren);
        response.put("monthChildren", monthChildren);
        response.put("classesName", classes.getName());
        response.put("planEarly", coursePlanClasses);
        return response;
    }


//    private ClassesChildrenListVo matchClassChild(ChildrenCourseItemDO item, List<ClassesChildrenListVo> classesChildren) {
//        Optional<ClassesChildrenListVo> vo = classesChildren.stream().filter(o -> !o.getId().equals(item.getChildrenId())).findAny();
//        return vo.isPresent() ? vo.get() : null;
//    }

    private ChildrenListDO matchAllChild(ClassesChildrenListVo child, List<ChildrenListDO> allChildren) {
        Optional<ChildrenListDO> vo = allChildren.stream().filter(o -> o.getId().equals(child.getId())).findAny();
        return vo.isPresent() ? vo.get() : null;
    }

    @Transactional
    @Override
    public Boolean planChildren(String coursePlanClassesId, List<String> childrenIdList) {
        if (Objects.isNull(coursePlanClassesId))
            throw new BusinessException(CHILDREN_HAS_NOT_COURSE_TIME, "所选课程有误");
        if (Objects.isNull(childrenIdList))
            throw new BusinessException(CHILDREN_HAS_NOT_COURSE_TIME, "请选中孩子");


        //检查课程数量是否足够
        List<ChildrenExtendInfo> extendInfoList = childrenExtendInfoRepository.findByChildrenIdIn(childrenIdList);
        for (ChildrenExtendInfo extendInfo : extendInfoList) {
            if (extendInfo.getEarlyLeftCourseNum() <= 0)
                throw new BusinessException(CHILDREN_HAS_NOT_COURSE_TIME, "宝宝剩余课程次数不足");
        }
        //删除已排宝宝
        childrenCourseItemRepository.deleteByCoursePlanId(coursePlanClassesId);
        List<ChildrenCourseItem> itemList = Lists.newArrayList();
        for (String childrenId : childrenIdList) {
            ChildrenCourse childrenCourse = childrenCourseRepository.findByChildrenId(childrenId);
            // 判定是否统一时间排多课
//            if (DateUtil.hasTimeUnionSet(plan1.getBeginTime(), plan1.getEndTime(), plan2.getBeginTime(), plan2.getEndTime())) {
//                String timeStr = DateUtil.millisToDateTime(plan1.getBeginTime().getTime(), "MM-dd") + " " + DateUtil.millisToDateTime(plan1.getBeginTime().getTime(), "HH:mm") + "-" + DateUtil.millisToDateTime(plan1.getEndTime().getTime(), "HH:mm");
//                throw new BusinessException(SAME_TIME_CHILDREN_DUPLICATE, String.format("同一时间段，宝宝课程不能重复, %s 重复", timeStr));
//            }


            ChildrenCourseItem item = ChildrenCourseItem.of(ChildrenCourseConstant.ItemStatus.NON_USED.codeOf(), childrenCourse.getId(), childrenId, null, coursePlanClassesId);
            itemList.add(item);
            // 评测记录
//            EvaluateRecord record = evaluateRecordRepository.findTop1ByChildrenIdOrderByCreatedDesc(childrenId);
//            item.setEvaluateRecordId(Objects.isNull(record) ? null : record.getId());
            //TODO feeds暂时不推。
//            Feeds feeds = feedsService.save(staffId, childrenId, areaId, "", FeedsConstant.PALN_COURSE, String.format("%s的课程安排", children.getName()), "", "", "", "");
        }

        childrenCourseItemRepository.saveAll(itemList);
        return true;
    }

    @Transactional
    @Override
    public List<CoursePlanClasses> submitPreWeekPlans(String date, String areaId) {
        Date lastWeekDay = DateUtil.getLaskWeekDay(date);
        Map map = DateUtil.getCurrentDayBeginAndEnd(DateUtil.millisToDateTime(lastWeekDay.getTime(), "yyyy-MM-dd"));
        Date begin = (Date) map.get("beg");
        Date end = (Date) map.get("end");
        List<CoursePlanClasses> list = Lists.newArrayList();
        List<CoursePlanClasses> coursePlanClassesList = coursePlanClassesRepository.findByAreaIdAndCategoryAndCourseDateBetween(areaId, CourseConstant.Category.EARLY.codeOf(), begin, end);
        for (CoursePlanClasses classes : coursePlanClassesList) {
            CoursePlanClasses newPlanClasses = CoursePlanClasses.ofOldCoursePlanClass(classes);
            newPlanClasses.setCourseDate(DateUtil.parseToDate(date));
            if (CourseConstant.CourseType.SAT.codeOf() == classes.getCourseType()) {
                Course newCourse = getNextCourseId(classes.getCourseId());
                newPlanClasses.setCourseId(newCourse.getId());
                newPlanClasses.setCourseName(newCourse.getName());
                newPlanClasses.setTypeName(newCourse.getTypeName());
            }
            coursePlanClassesRepository.save(newPlanClasses);
            List<ChildrenCourseItemDO> planedChildren = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(classes.getId());
            List<String> childrenIds = planedChildren.stream().map(o -> o.getChildrenId()).collect(Collectors.toList());
            planChildren(newPlanClasses.getId(), childrenIds);
            list.add(newPlanClasses);
        }
        return list;
    }

    private Course getNextCourseId(String courseId) {
        Course course = courseRepository.getOne(courseId);

        Integer nextWeek = course.getWeek() + 1;
        Course newCourse = courseRepository.findByTypeAndTypeNameAndWeek(CourseConstant.CourseType.SAT.codeOf(), course.getTypeName(), nextWeek);
        return Objects.isNull(newCourse) ? course : newCourse;
    }


    @Override
    public Page<AreaCourseListVo> getCoursePageByArea(AreaCourseQuery query) {
        query.setTodayBegin(DateUtil.millisToDateTime(new Date().getTime(), "yyyy-MM-dd"));
        List<Object[]> coursePlans = coursePlanTeachingAreaRepository.findCoursePlanByAreaId(query);
        List<AreaCourseListVo> areaCourseList = Lists.newArrayList();
        coursePlans.forEach(o -> {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(o[0].toString());
            StaffDTO staff = ssoCallService.getStaffDTO(null, o[7].toString());
            areaCourseList.add(AreaCourseListVo.of(o, itemList, staff));
        });
        Long count = coursePlanTeachingAreaRepository.countCoursePlanByAreaId(query);
        return new PageImpl<AreaCourseListVo>(areaCourseList, PageRequest.of(query.getPageNum(), query.getPageSize()), count);
    }

    @Override
    public List<WebCourseListVo> getCourseListByArea(String areaId, String todayBegin) {
        List<Object[]> coursePlans = coursePlanTeachingAreaRepository.findCoursePlanListByAreaId(areaId, todayBegin);
        List<WebCourseListVo> webCourseList = Lists.newArrayList();
        coursePlans.forEach(o -> {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(o[0].toString());
            StaffDTO staff = ssoCallService.getStaffDTO(null, o[6].toString());
            webCourseList.add(WebCourseListVo.of(o, itemList, staff));
        });
        return webCourseList;
    }

    @Override
    public Page<PadTeacherCoursePlanListVo> getPadAllCoursePlan(ClassesCoursePlanQuery query, Boolean isTestUser) {
        if (isTestUser) {
            query.setPageNum(0);
            //size==0，查出所有的数据，避免查出有宝宝的早教排课<10条
            query.setPageSize(0);
        }

        String today = DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd");
        query.setBeginDate(today);
        query.setCourseCategory(CourseConstant.Category.EARLY.codeOf());
        List<CoursePlanClassesDO> coursePlanClassesList = classesCoursePlanRepository.findByConditions(query);
        Long count = classesCoursePlanRepository.countByConditions(query);


        //FIXME 因为不显示没有排宝宝的课，分页处理不正确，临时这样处理。客户端只是去了list, 没有管任何分页参数
        List<PadTeacherCoursePlanListVo> list = Lists.newArrayList();
        for (CoursePlanClassesDO c : coursePlanClassesList) {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(c.getClassesCoursePlanId());
            if (Objects.nonNull(itemList) && itemList.size() != 0) {
                PadTeacherCoursePlanListVo vo = PadTeacherCoursePlanListVo.of(c, itemList);
                list.add(vo);
            }
        }
        if (isTestUser) {
            //外部用户，只返回前10条有宝宝的早教排课
            List<PadTeacherCoursePlanListVo> collect = list.stream().limit(10).collect(Collectors.toList());
            return new PageImpl<>(collect, PageRequest.of(0, 10), collect.size());
        }
        return new PageImpl<>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), count);
    }

    @Override
    public Page<PadTeacherCoursePlanListVo> getPadHistoryCoursePlan(ClassesCoursePlanQuery query, Boolean isTestUser) {
        if (isTestUser) {
            query.setPageNum(0);
            //size==0，查出所有的数据，避免查出有宝宝的早教排课<10条
            query.setPageSize(0);
        }
        String today = DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd");
        query.setEndDate(today);
        query.setCourseCategory(CourseConstant.Category.EARLY.codeOf());
        List<CoursePlanClassesDO> coursePlanClassesList = classesCoursePlanRepository.findHistoryByCondition(query);
        Long count = classesCoursePlanRepository.countHistoryByCondition(query);

        List<PadTeacherCoursePlanListVo> list = Lists.newArrayList();
        for (CoursePlanClassesDO c : coursePlanClassesList) {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(c.getClassesCoursePlanId());
            PadTeacherCoursePlanListVo vo = PadTeacherCoursePlanListVo.of(c, itemList);
            list.add(vo);
        }
        if (isTestUser) {
            //外部用户，只返回前10条有宝宝的早教排课
            List<PadTeacherCoursePlanListVo> collect = list.stream().limit(10).collect(Collectors.toList());
            return new PageImpl<>(collect, PageRequest.of(0,10), collect.size());
        }
        return new PageImpl<>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), count);
    }

    @Override
    public PadCoursePlanDetailVo getEarlyCourseDetail(String coursePlanClassesId) {
        CoursePlanClasses coursePlanClasses = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Course course = courseRepository.getOne(coursePlanClasses.getCourseId());

        List<AdviseDetailDO> adviseList = classesChildrenRepository.findAdviseByClasses(coursePlanClasses.getClassesId());//查询班级学生评测建议
        List<TeacherDO> teacherList = teacherService.getTeacherByClasses(coursePlanClasses.getClassesId());
        Classes classes = classesRepository.getOne(coursePlanClasses.getClassesId());

        Object template = null;
        if(course.getTemplateType() == CourseConstant.Template.AREA_BUILD.codeOf()){
            CourseTemplateAreaBuild courseTemplate3 = courseTemplate3Repository.getOne(course.getCourseTemplateId());
            PrepareCourse prepareCourse = prepareCourseRepository.findByCoursePlanClassesId(coursePlanClasses.getId());

            if (Objects.nonNull(prepareCourse) && CourseNurseryConstant.PrepareStatus.PREPARED.codeOf() == coursePlanClasses.getPrepareStatus() || CourseNurseryConstant.PrepareStatus.APPROVED.codeOf() == coursePlanClasses.getPrepareStatus()) {
                //TODO: 已备课对象要叠加备课信息
//                PrepareTemplate3 prepareTemplate3 = prepareTemplate3Repository.findByPrepareCourseId(prepareCourse.getId());
//                template = PadCourseTemplate3Vo.ofPrepareCourse(prepareCourse, prepareTemplate3);
            } else
                template = PadCourseTemplate3Vo.ofCourse(coursePlanClasses, courseTemplate3);

        }

        List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(coursePlanClasses.getId());

        PadCoursePlanDetailVo vo = PadCoursePlanDetailVo.ofEarly(course.getTemplateType(), coursePlanClasses, itemList, adviseList, teacherList, template, classes.getName());
        return vo;
    }

    @Transactional
    @Override
    public Boolean beginCourse(StaffDTO staff, List<Map> childrenCourseStatusDtoList, String[] teacherIds, String coursePlanClassesId) {
        CoursePlanClasses coursePlanClasses = coursePlanClassesRepository.getOne(coursePlanClassesId);
        if (CourseEarlyConstant.CourseStatus.STARTING.codeOf() != coursePlanClasses.getCourseStatus())
            throw new BusinessException(COURSE_PLAN_EARLY_PROCESSED, "早教课程已经处理");

        coursePlanClasses.setCourseStatus(CourseEarlyConstant.CourseStatus.PROCESSED.codeOf());
        coursePlanClasses.setBeginCourseTeacherId(staff.getId());
        coursePlanClasses.setBeginCourseTime(new Date());
        coursePlanClassesRepository.save(coursePlanClasses);

        List<RelTeacherCoursePlanAttended> teacherCoursePlanNurseryList = org.assertj.core.util.Lists.newArrayList();
        for (String id : teacherIds) {
            List<StaffDTO> staffDTOList = ssoCallService.getStaffDTOList(null, teacherIds);
            teacherCoursePlanNurseryList.add(RelTeacherCoursePlanAttended.of(id, coursePlanClasses.getId(), StaffDTOUtils.matchOf(id, staffDTOList).getRealName()));
        }
        for (Map m : childrenCourseStatusDtoList) {
            childrenCourseService.checkIn(staff, m.get("childrenCourseItemId").toString(), Integer.valueOf(m.get("status").toString()), "");
        }

        //处理老师课程表历史记录
        teacherService.precessCourse(coursePlanClassesId);
        return true;
    }

    @Override
    public Page<PadTeacherCoursePlanListVo> getPadTodayCoursePlan(ClassesCoursePlanQuery query, Boolean isTestUser) {
        if (isTestUser) {
            query.setPageNum(0);
            //size==0，查出所有的数据，避免查出有宝宝的早教排课<10条
            query.setPageSize(0);
        }

        String today = DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd");
        query.setBeginDate(today);
        query.setCourseCategory(CourseConstant.Category.EARLY.codeOf());
        List<CoursePlanClassesDO> coursePlanClassesList = classesCoursePlanRepository.findTodayCourseSelfByConditions(query);
     //   Long count = classesCoursePlanRepository.countByConditions(query);


        //FIXME 因为不显示没有排宝宝的课，分页处理不正确，临时这样处理。客户端只是去了list, 没有管任何分页参数
        List<PadTeacherCoursePlanListVo> list = Lists.newArrayList();
        for (CoursePlanClassesDO c : coursePlanClassesList) {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(c.getClassesCoursePlanId());
            if (Objects.nonNull(itemList) && itemList.size() != 0) {
                PadTeacherCoursePlanListVo vo = PadTeacherCoursePlanListVo.of(c, itemList);
                list.add(vo);
            }
        }

        return new PageImpl<>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), list.size());
    }

    @Override
    public Page<PadTeacherCoursePlanListVo> getPadWeekCoursePlan(ClassesCoursePlanQuery query, Boolean isTestUser) {
        if (isTestUser) {
            query.setPageNum(0);
            //size==0，查出所有的数据，避免查出有宝宝的早教排课<10条
            query.setPageSize(0);
        }

        String weekStartDay = DateUtil.getWeekStartDay();
        String weekEndDay = DateUtil.getWeekEndDay();
        query.setBeginDate(weekStartDay);
        query.setEndDate(weekEndDay);
        query.setCourseCategory(CourseConstant.Category.EARLY.codeOf());
        List<CoursePlanClassesDO> coursePlanClassesList = classesCoursePlanRepository.findWeekCourseByConditions(query);
       // Long count = classesCoursePlanRepository.countByConditions(query);


        //FIXME 因为不显示没有排宝宝的课，分页处理不正确，临时这样处理。客户端只是去了list, 没有管任何分页参数
        List<PadTeacherCoursePlanListVo> list = Lists.newArrayList();
        for (CoursePlanClassesDO c : coursePlanClassesList) {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(c.getClassesCoursePlanId());
            if (Objects.nonNull(itemList) && itemList.size() != 0) {
                PadTeacherCoursePlanListVo vo = PadTeacherCoursePlanListVo.of(c, itemList);
                list.add(vo);
            }
        }
        if (isTestUser) {
            //外部用户，只返回前10条有宝宝的早教排课
            List<PadTeacherCoursePlanListVo> collect = list.stream().limit(10).collect(Collectors.toList());
            return new PageImpl<>(collect, PageRequest.of(0, 10), collect.size());
        }
        return new PageImpl<>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), list.size());
    }

    @Override
    public Page<PadTeacherCoursePlanListVo> getPadNextWeekCoursePlan(ClassesCoursePlanQuery query, Boolean isTestUser) {
        if (isTestUser) {
            query.setPageNum(0);
            //size==0，查出所有的数据，避免查出有宝宝的早教排课<10条
            query.setPageSize(0);
        }

        String weekStartDay = DateUtil.formatDayToStr(DateUtil.getBeginDayOfNextWeek());
        String weekEndDay = DateUtil.formatDayToStr(DateUtil.getEndDayOfNextWeek());
        query.setBeginDate(weekStartDay);
        query.setEndDate(weekEndDay);
        query.setCourseCategory(CourseConstant.Category.EARLY.codeOf());
        List<CoursePlanClassesDO> coursePlanClassesList = classesCoursePlanRepository.findWeekCourseByConditions(query);


        //FIXME 因为不显示没有排宝宝的课，分页处理不正确，临时这样处理。客户端只是去了list, 没有管任何分页参数
        List<PadTeacherCoursePlanListVo> list = Lists.newArrayList();
        for (CoursePlanClassesDO c : coursePlanClassesList) {
            List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(c.getClassesCoursePlanId());
            if (Objects.nonNull(itemList) && itemList.size() != 0) {
                PadTeacherCoursePlanListVo vo = PadTeacherCoursePlanListVo.of(c, itemList);
                list.add(vo);
            }
        }
        if (isTestUser) {
            //外部用户，只返回前10条有宝宝的早教排课
            List<PadTeacherCoursePlanListVo> collect = list.stream().limit(10).collect(Collectors.toList());
            return new PageImpl<>(collect, PageRequest.of(0, 10), collect.size());
        }
        return new PageImpl<>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), list.size());
    }


}
